package com.memory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.memory.common.BaseContext;
import com.memory.common.R;
import com.memory.daomain.ShoppingCart;
import com.memory.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 邓哈哈
 * 2023/2/1 14:31
 * Function:
 * Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加商品
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);

        //1.设置用户id,指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //2.查询当前菜品或者套餐是否在当前用户购物车中
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        if (dishId != null) {//添加到购物车的是菜品
            lqw.eq(ShoppingCart::getUserId, currentId);
            lqw.eq(ShoppingCart::getDishId, dishId);

        } else if (setmealId != null) {//添加到购物车的是套餐
            lqw.eq(ShoppingCart::getUserId, currentId);
            lqw.eq(ShoppingCart::getSetmealId, setmealId);

        } else {
            return R.error("未知错误");
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);

        //3.如果存在,就在原来的数量基础上加一
        boolean save;

        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);//设置商品数量
            shoppingCart.setCreateTime(LocalDateTime.now());//设置下单日期
            //修改购物车内原有物品的数量number
            save = shoppingCartService.updateById(one);

        } else { //4.如果不存在,则添加到购物车,数量默认是一
            shoppingCart.setNumber(1);//设置商品数量
            shoppingCart.setCreateTime(LocalDateTime.now());//设置下单日期
            //执行添加
            save = shoppingCartService.save(shoppingCart);
        }

        return save ? R.success("成功添加至购物车") : R.error("添加至购物车失败");
    }

    @PostMapping("/sub")
    public R<String> sub() {
        return null;
    }

    /**
     * 购物车陈列商品
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);

        return shoppingCartList != null ? R.success(shoppingCartList) : R.error("未知错误");
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        boolean remove = shoppingCartService.remove(lqw);

        return remove ? R.success("清空成功") : R.error("清空失败,请稍后重试");
    }


}
